/*
 * DivulgarNotas.java
 *
 * Created on 15 de Junho de 2010, 01:28
 */

package virtual.view;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import virtual.BD.Prova;
import virtual.service.ProvaJpaController;
import virtual.service.exceptions.NonexistentEntityException;

/**
 *
 * @author  onyas
 */
public class DivulgarNotas extends javax.swing.JPanel {

    
    private ProvaJpaController service;
    
    Prova prova1 = new Prova();
    Prova prova2 = new Prova();
    Prova prova3 = new Prova();
    
    
    /** Creates new form DivulgarNotas */
    public DivulgarNotas() {
        initComponents();
        
        service = new ProvaJpaController();
        
        preencherNotasAluno();
    }
    
    
    
    public String calculaMedia() {
        
        double nota1 = jtfAvaliacao1.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao1.getText());
        double nota2 = jtfAvaliacao2.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao2.getText());
        double nota3 = jtfAvaliacao3.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao3.getText());
        double media = (nota1 + nota2 + nota3) / 3;
        
        String valor = media+"";
        if (valor.length() > 4)
            valor = valor.substring(0, 4);
        
        return valor;
        
    } // Fim do método calculaMedia
    
    
    public void preencherNotasAluno() {
        
        List<Prova> listaProva = service.findProvaEntities(jcbAluno.getSelectedIndex());
        
        if (listaProva.size() >= 3)
        {
            prova1.setCdProva(listaProva.get(0).getCdProva());
            prova2.setCdProva(listaProva.get(1).getCdProva());
            prova3.setCdProva(listaProva.get(2).getCdProva());
            
            jtfAvaliacao1.setText(listaProva.get(0).getVlNota() + "");
            jtfAvaliacao2.setText(listaProva.get(1).getVlNota() + "");
            jtfAvaliacao3.setText(listaProva.get(2).getVlNota() + "");
        } else if (listaProva.size() >= 2) {
            prova1.setCdProva(listaProva.get(0).getCdProva());
            prova2.setCdProva(listaProva.get(1).getCdProva());
            
            jtfAvaliacao1.setText(listaProva.get(0).getVlNota() + "");
            jtfAvaliacao2.setText(listaProva.get(1).getVlNota() + "");
        } else if (listaProva.size() >= 1) {
            prova1.setCdProva(listaProva.get(0).getCdProva());
            jtfAvaliacao1.setText(listaProva.get(0).getVlNota() + "");
        }

        jtfMedia.setText(calculaMedia());
        jtfMedia.repaint();
        
    } // Fim do método preencherNotasAluno

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlAluno = new javax.swing.JLabel();
        jcbAluno = new javax.swing.JComboBox();
        jlAvaliacao1 = new javax.swing.JLabel();
        jtfAvaliacao1 = new javax.swing.JTextField();
        jlAvaliacao2 = new javax.swing.JLabel();
        jtfAvaliacao2 = new javax.swing.JTextField();
        jlAvaliacao3 = new javax.swing.JLabel();
        jtfAvaliacao3 = new javax.swing.JTextField();
        jlMedia = new javax.swing.JLabel();
        jtfMedia = new javax.swing.JTextField();
        jbTurma = new javax.swing.JButton();
        jbSalvar = new javax.swing.JButton();

        jlAluno.setText("Aluno");
        jlAluno.setName("jlAluno"); // NOI18N

        jcbAluno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diego Armando Maradona", "Ferenc Puskas", "Franz Beckembauer", "Johan Cruyff", "Marco Van Basten", "Roberto Baggio", "Ruud Gullit" }));
        jcbAluno.setName("jcbAluno"); // NOI18N
        jcbAluno.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbAlunoItemStateChanged(evt);
            }
        });

        jlAvaliacao1.setText("Avaliação 1");
        jlAvaliacao1.setName("jlAvaliacao1"); // NOI18N

        jtfAvaliacao1.setName("jtfAvaliacao1"); // NOI18N
        jtfAvaliacao1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfAvaliacao1CaretUpdate(evt);
            }
        });

        jlAvaliacao2.setText("Avaliação 2");
        jlAvaliacao2.setName("jlAvaliacao2"); // NOI18N

        jtfAvaliacao2.setName("jtfAvaliacao2"); // NOI18N
        jtfAvaliacao2.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfAvaliacao2CaretUpdate(evt);
            }
        });

        jlAvaliacao3.setText("Avaliação 3");
        jlAvaliacao3.setName("jlAvaliacao3"); // NOI18N

        jtfAvaliacao3.setName("jtfAvaliacao3"); // NOI18N
        jtfAvaliacao3.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfAvaliacao3CaretUpdate(evt);
            }
        });

        jlMedia.setText("Média");
        jlMedia.setName("jlMedia"); // NOI18N

        jtfMedia.setEnabled(false);
        jtfMedia.setName("jtfMedia"); // NOI18N

        jbTurma.setText("Turma");
        jbTurma.setName("jbTurma"); // NOI18N

        jbSalvar.setText("Salvar");
        jbSalvar.setName("jbSalvar"); // NOI18N
        jbSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfAvaliacao1, 0, 0, Short.MAX_VALUE)
                    .addComponent(jlAvaliacao1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfAvaliacao2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jlAvaliacao2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfAvaliacao3)
                    .addComponent(jbTurma, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlAvaliacao3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jlMedia))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtfMedia, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlAvaliacao3)
                            .addComponent(jlMedia))
                        .addGap(33, 33, 33))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jlAluno)
                                .addComponent(jlAvaliacao1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jcbAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfAvaliacao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jlAvaliacao2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jtfAvaliacao2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfAvaliacao3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbTurma)
                    .addComponent(jbSalvar))
                .addContainerGap(43, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jtfAvaliacao1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfAvaliacao1CaretUpdate
    
    jtfMedia.setText(calculaMedia());
    jtfMedia.repaint();
    
}//GEN-LAST:event_jtfAvaliacao1CaretUpdate

private void jtfAvaliacao2CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfAvaliacao2CaretUpdate
    
    jtfMedia.setText(calculaMedia());
    jtfMedia.repaint();
    
}//GEN-LAST:event_jtfAvaliacao2CaretUpdate

private void jtfAvaliacao3CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfAvaliacao3CaretUpdate
    
    jtfMedia.setText(calculaMedia());
    jtfMedia.repaint();
    
}//GEN-LAST:event_jtfAvaliacao3CaretUpdate

private void jcbAlunoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbAlunoItemStateChanged
    
    jtfAvaliacao1.setText("");
    jtfAvaliacao2.setText("");
    jtfAvaliacao3.setText("");
    
    preencherNotasAluno();
    
}//GEN-LAST:event_jcbAlunoItemStateChanged

private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
    
    try {

        

        prova1.setVlNota(jtfAvaliacao1.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao1.getText()));
        prova2.setVlNota(jtfAvaliacao2.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao2.getText()));
        prova3.setVlNota(jtfAvaliacao3.getText().equals("") ? 0.0 : Double.parseDouble(jtfAvaliacao3.getText()));
        
        
        prova1.setCdAluno(jcbAluno.getSelectedIndex());
        prova2.setCdAluno(jcbAluno.getSelectedIndex());
        prova3.setCdAluno(jcbAluno.getSelectedIndex());
        
        prova1.setCdAvaliacao(1);
        prova2.setCdAvaliacao(2);
        prova3.setCdAvaliacao(3);
        

        /*if (prova1.getCdProva() == null) {
            service.create(prova1);
            service.create(prova2);
            service.create(prova3);
            JOptionPane.showMessageDialog(this, "Notas cadastradas com sucesso!", "Salvar", JOptionPane.INFORMATION_MESSAGE);
        } else {*/
            try {
                service.edit(prova1);
                service.edit(prova2);
                service.edit(prova3);
                JOptionPane.showMessageDialog(this, "Notas alteradas com sucesso!", "Alterar", JOptionPane.INFORMATION_MESSAGE);
            } catch (NonexistentEntityException nee) {
                Logger.getLogger(CadastroPessoa.class.getName()).log(Level.SEVERE, null, nee);
            } catch (Exception e) {
                Logger.getLogger(CadastroPessoa.class.getName()).log(Level.SEVERE, null, e);
            } // Fim do try/catch
        //} // Fim do if/else
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Não foi possível salvar.", "Erro ao divulgar nota!", JOptionPane.ERROR_MESSAGE);
        System.err.println("ERRO em CadastroTurma.btnCadastrarTurmaActionPerformed(java.awt.event.ActionEvent):\n" + e);
    } // Fim do try/catch
    
}//GEN-LAST:event_jbSalvarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbSalvar;
    private javax.swing.JButton jbTurma;
    private javax.swing.JComboBox jcbAluno;
    private javax.swing.JLabel jlAluno;
    private javax.swing.JLabel jlAvaliacao1;
    private javax.swing.JLabel jlAvaliacao2;
    private javax.swing.JLabel jlAvaliacao3;
    private javax.swing.JLabel jlMedia;
    private javax.swing.JTextField jtfAvaliacao1;
    private javax.swing.JTextField jtfAvaliacao2;
    private javax.swing.JTextField jtfAvaliacao3;
    private javax.swing.JTextField jtfMedia;
    // End of variables declaration//GEN-END:variables

}
